package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwebT;
import cn.e4j.modular.crm.service.GwebTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 料号与客户零件关系管理
 * Created by shixc on 2019-1-17
 */
@Controller
@RequestMapping("/crm/gwei002")
public class Gwei002Controller extends BaseController {

    @Autowired
    private GwebTService gwebTService;

    /**
     * 跳转料号与客户零件关系管理页面
     */
    @RequiresPermissions("crm:gwei002:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwei002.html";
    }

    /**
     * 查询料号与客户零件信息列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwei002:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwebTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwei002:init")
    @ResponseBody
    public ResponseData add(GwebT gwebT) {
        gwebTService.add(gwebT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwei002:init")
    @ResponseBody
    public ResponseData edit(GwebT gwebT) {
        gwebTService.edit(gwebT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwei002:init")
    @ResponseBody
    public ResponseData del(GwebT gwebT) {
        gwebTService.delete(gwebT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwei002:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwebT> gwebTList = inDto.getReqList(GwebT.class);
        gwebTService.delDataBatch(gwebTList);
        return SUCCESS_TIP;
    }

    /**
     * 获取客户资料列表
     */
    @ResponseBody
    @RequestMapping("/getGwakList")
    public Dto getGwakList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAKENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwebTService.getGwakList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取所属法人列表
     */
    @ResponseBody
    @RequestMapping("/getOrgList")
    public Dto getOrgList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        List<Map<String, Object>> list = gwebTService.getOrgList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    @RequestMapping(value = "/importsData", method = RequestMethod.POST)
    @RequiresPermissions("crm:gwei002:init")
    @ResponseBody
    public ResponseData importsUser(MultipartFile file, ModelMap map) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = Dtos.newDto();
        ImportParams params = new ImportParams();
        params.setTitleRows(0);//表格标题行数,默认0
        params.setHeadRows(1);//表头行数,默认1
        params.setStartRows(0);//字段真正值和列标题之间的距离 默认0
        int count = 0;
        try {
            List<GwebT> list = ExcelImportUtil.importExcel(file.getInputStream(), GwebT.class, params);
            for (GwebT gwebT : list) {
                if (gwebT.getGWEB002() != null && gwebT.getGWEB004() != null && gwebT.getGWEB007() != null) {
                    Dto dto = Dtos.newDto();
                    dto.put("GWAKENT", user.getDBC());
                    dto.put("GWAK001", gwebT.getGWEB002());
                    dto.put("lang", LangUtils.getLang(user.getLANG()));
                    Dto gwak = gwebTService.getGwak(dto);
                    Dto pDto = Dtos.newDto();
                    pDto.put("GWAMENT", user.getDBC());
                    pDto.put("GWAM001", gwebT.getGWEB004());
                    pDto.put("lang", LangUtils.getLang(user.getLANG()));
                    Dto gwam = gwebTService.getGwam(pDto);
                    if (gwak != null && gwam != null) {
                        if (gwak.size() > 0 && gwam.size() > 0) {
                            gwebT.setGWEB003(gwak.getString("GWAKL004"));
                            gwebT.setGWEB005(gwam.getString("GWAML003"));
                            gwebT.setGWEB006(gwam.getString("GWAML004"));
                            gwebT.setGWEB011("Y");
                            List<Dto> sList = gwebTService.getOrg(inDto);
                            if (sList != null) {
                                gwebT.setGWEBPLANT(sList.get(0).getString("ID"));
                            }
                            gwebTService.add(gwebT);
                            count++;
                        }
                    } else if (gwak == null) {
                        return new ErrorResponseData(402,
                                "客户:" + gwebT.getGWEB002() + "在客户表中未查询到!");
                    } else if (gwam == null) {
                        return new ErrorResponseData(402,
                                "商品:" + gwebT.getGWEB004() + "在商品表中未查询到!");
                    }
                } else {
                    continue;
                }
            }
            if (count == 0) {
                return new ErrorResponseData(401,
                        "所有导入数据都不符合要求!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData(BizExceptionEnum.SERVER_ERROR.getCode(),
                    BizExceptionEnum.SERVER_ERROR.getMessage());
        }
        return SUCCESS_TIP;
    }
}
